//
//  RBGContactController.m
//  BateauGenerique
//
//  Created by Kevin De Jesus Ferreira on 23/05/2014.
//  Copyright (c) 2014 Kevin De Jesus Ferreira. All rights reserved.
//

#import "RBGContactController.h"
#import "RBGNavigationBar.h"
#import "RBGAppDelegate.h"
#import "RBGColorManager.h"
#import "UIImageView+WebCache.h"
#import "RBGServiceContact.h"
#import "Client.h"
#import <MessageUI/MessageUI.h>
#import "RBGEmailController.h"

@interface RBGContactController ()<MKMapViewDelegate,RBGServiceContactDelegate,UIAlertViewDelegate,MFMailComposeViewControllerDelegate>{
    RBGAppDelegate *appDelegate;
    RBGServiceContact  * serviceContact;
    Client * client;
}


@end

@implementation RBGContactController


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self charger];
    [self remplir];
    [self positionnerVue];
    [self chargerCouleur];
}

-(void) charger
{
    self.carte.delegate = self;
    appDelegate = (RBGAppDelegate *)[[UIApplication sharedApplication] delegate];
    serviceContact = [[RBGServiceContact alloc]init];
    serviceContact.delegate = self;
    [serviceContact getContact];
    
        [RBGNavigationBar customNavigationBar:self.navigationController andTitle:@"Contact" andBackgroundColor:appDelegate.donnees.parametres.backgroundColorUn andTextColor:appDelegate.donnees.parametres.fontColorUn];
}

-(void) remplir
{
    
    [self.logo setImageWithURL:[NSURL URLWithString:appDelegate.donnees.parametres.imageLogo]];
    [self.fond setImageWithURL:[NSURL URLWithString:appDelegate.donnees.parametres.imageFond]];
    
}

-(void) positionnerVue
{
    
    [self.logo setFrame:CGRectMake(120, 10, 100, 80)];
    
    [self.descriptionSociete setFrame:CGRectMake(10, self.logo.frame.origin.y + self.logo.frame.size.height, self.descriptionSociete.frame.size.width, self.descriptionSociete.frame.size.height)];
    
    [self.buttons setFrame:CGRectMake(0, self.descriptionSociete.frame.origin.y  + self.descriptionSociete.frame.size.height, self.buttons.frame.size.width, self.buttons.frame.size.height)];
    
    [self.carte setFrame:CGRectMake(0, self.buttons.frame.origin.y  + self.buttons.frame.size.height, 320, self.carte.frame.size.height +100)];
    
    
}

-(void) chargerCouleur
{
    self.descriptionSociete.backgroundColor = [UIColor clearColor];
    
    [RBGColorManager changeTextColorSubViews:self.descriptionSociete withColor:appDelegate.donnees.parametres.backgroundColorDeux];
    
    // Mettre surement pour tout les states
    self.buttonCall.backgroundColor = appDelegate.donnees.parametres.backgroundColorDeux;
    self.buttonMail.backgroundColor = appDelegate.donnees.parametres.backgroundColorDeux;
    
    self.buttonCall.tintColor = appDelegate.donnees.parametres.backgroundColorUn;
    self.buttonMail.tintColor = appDelegate.donnees.parametres.backgroundColorUn;
    
    [self.bonhomme setImage:[RBGColorManager colorImageWithColor:appDelegate.donnees.parametres.fontColorDeux andImage:self.bonhomme.image]];
    
    [self.iconCall setImage:[RBGColorManager colorImageWithColor:appDelegate.donnees.parametres.fontColorDeux andImage:self.iconCall.image]];
    
    [self.iconMail setImage:[RBGColorManager colorImageWithColor:appDelegate.donnees.parametres.fontColorDeux andImage:self.iconMail.image]];
    // Label button
    self.textAppeler.textColor = appDelegate.donnees.parametres.fontColorDeux;
    self.textMail.textColor = appDelegate.donnees.parametres.fontColorDeux;
    
    [RBGColorManager changeTextColorSubViews:self.descriptionSociete withColor:appDelegate.donnees.parametres.fontColorDeux];
    
    [self.grandCercle setImage:[RBGColorManager colorImageWithColor:[UIColor whiteColor] andImage:self.grandCercle.image]];
    [self.petitCercle setImage:[RBGColorManager colorImageWithColor:appDelegate.donnees.parametres.backgroundColorUn andImage:self.petitCercle.image]];
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    NSString * location = [NSString stringWithFormat:@"%@, %@, %@",client.adresse,client.ville,client.cp];
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:location
                 completionHandler:^(NSArray* placemarks, NSError* error){
                     if (placemarks && placemarks.count > 0) {
                         CLPlacemark *topResult = [placemarks objectAtIndex:0];
                         MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
                         
                         MKCoordinateRegion region = self.carte.region;
                         region.center = [(CLCircularRegion *)placemark.region center];
                         region.span.longitudeDelta /= 300.0;
                         region.span.latitudeDelta /= 300.0;
                         
                         [self.carte setRegion:region animated:YES];
                         [self.carte addAnnotation:placemark];
                     }
                 }
     ];
    
}

- (IBAction)contacterPhone:(id)sender {
    
    if(client != nil && client.tel1 != nil && client.tel1 >0){
        
        UIAlertView *myAlert = [[UIAlertView alloc]
                                initWithTitle:@"Appeller"
                                message:[NSString stringWithFormat:@"Souhaitez vous appeler le %@ ?",client.tel1 ]
                                delegate:self
                                cancelButtonTitle:@"Non"
                                otherButtonTitles:@"Oui",nil];
        myAlert.tag = 0;
        [myAlert show];
        
    }
}

- (IBAction)appeler:(id)sender {
    NSLog(@"Appeller");
    
    [self contacterPhone:nil];
    
    /*
     if(client != nil && client.tel1 != nil && client.tel1 >0){
     
     
     if(client.tel2 != nil && client.tel2.length > 0){
     UIAlertView *alert = [[UIAlertView alloc]
     
     initWithTitle:@"Appeler"
     message:@"Veuillez choisir le numéro"
     delegate:self
     cancelButtonTitle:@"Annuler"
     otherButtonTitles:client.tel1,client.tel2,nil];
     [alert show];
     }else{
     NSString *phoneNumber = [@"tel://" stringByAppendingString:client.tel1];
     
     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
     }
     }
     */
}

- (IBAction)mail:(id)sender {
    
    if(client != nil && client.email != nil && client.email.length > 0){
        NSLog(@"Mail");
        // Email Subject
        NSString *emailTitle = [NSString stringWithFormat:@"Mail provenant de votre appli iOS %@",client.nom];
        // Email Content
        NSString *messageBody = @"Bonjour,\n\nJe souhaiterai avoir des informations complémentaires \nMerci de me recontacter par mail.\nCordialement";
        
        RBGEmailController * emailController = [[RBGEmailController alloc]init];
        
        emailController.message = messageBody;
        
        emailController.titre = emailTitle;
        emailController.toRecipents =[NSArray arrayWithObject:client.email];
        emailController.clidirect = @"1";
        [self.navigationController pushViewController:emailController animated:YES];
        
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(alertView.tag == 0){
        if(buttonIndex == 1){
            if(client!= nil && client.tel1 != nil){
                NSString *phoneNumber = [@"tel://" stringByAppendingString:client.tel1];
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
            }
        }
    }
}

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            break;
        default:
            break;
    }
    
    // Close the Mail Interface
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (void) receiveDonneesContact:(id)donnees
                      andType :(int)typeDonnees{
    
    client = (Client *) donnees;
    [self.nomSociete setText:client.nom];
    [self.nomRue setText:client.adresse];
    [self.postCodeAndVille setText:[NSString stringWithFormat:@"%@ %@",client.departementNum,client.departement ]];
    [self.horaires setText:client.horaires];
    [self mapView:self.carte didUpdateUserLocation:nil];
    
}


- (void) receiveErreurContact:(int)statut{
    // Il y a une erreur et alors ?
}

- (IBAction)changeClearColor:(UIButton *)sender
{
    [sender setBackgroundColor:appDelegate.donnees.parametres.backgroundColorDeux];
    
}
- (IBAction)changeColor:(id)sender{
    
    [sender setBackgroundColor:appDelegate.donnees.parametres.backgroundColorUn];
}

@end
